//package com.nb.camerademo.test1;
//
//import android.Manifest;
//import android.content.pm.PackageManager;
//import android.graphics.ImageFormat;
//import android.graphics.SurfaceTexture;
//import android.hardware.camera2.CameraAccessException;
//import android.hardware.camera2.CameraCaptureSession;
//import android.hardware.camera2.CameraDevice;
//import android.hardware.camera2.CameraManager;
//import android.hardware.camera2.CaptureRequest;
//import android.media.Image;
//import android.media.ImageReader;
//
//import androidx.annotation.NonNull;
//import androidx.core.app.ActivityCompat;
//
//import com.nb.camerademo.BaseApp;
//
//import java.nio.ByteBuffer;
//import java.util.Arrays;
//
///**
// * @author Administrator on 2024-05-23.
// * <p>
// * 站在顶峰 看世界
// * 落到谷底 思人生
// */
//public class Camera2PhotoCapture implements CameraCaptureSession.CaptureCallback {
//
//    private CameraManager cameraManager;
//    private CameraDevice cameraDevice;
//    private CameraCaptureSession captureSession;
//
//    public Camera2PhotoCapture(CameraManager cameraManager) {
//        this.cameraManager = cameraManager;
//    }
//
//    public void openCamera(String cameraId) {
//        // 打开相机的代码（需要处理权限和异常）
//        if (ActivityCompat.checkSelfPermission(BaseApp.getApp(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
//            // TODO: Consider calling
//            //    ActivityCompat#requestPermissions
//            // here to request the missing permissions, and then overriding
//            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
//            //                                          int[] grantResults)
//            // to handle the case where the user grants the permission. See the documentation
//            // for ActivityCompat#requestPermissions for more details.
//            return;
//        }
//        try {
//            cameraManager.openCamera(cameraId, new CameraDevice.StateCallback() {
//                @Override
//                public void onOpened(@NonNull CameraDevice camera) {
//                    cameraDevice = camera;
//                    // 开始预览
//                    startCaptureSession();
//                }
//
//                @Override
//                public void onDisconnected(@NonNull CameraDevice camera) {
//                    // 处理相机断开的情况
//                }
//
//                @Override
//                public void onError(@NonNull CameraDevice camera, int error) {
//                    // 处理相机打开错误的情况
//                }
//            }, null);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//
//    private void startCaptureSession() {
//        try {
//            // 创建一个预览的Surface
//            SurfaceTexture surfaceTexture = new SurfaceTexture();
//            CaptureRequest.Builder captureRequestBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
//            captureRequestBuilder.addTarget(surfaceTexture);
//            // 创建CameraCaptureSession
//            cameraDevice.createCaptureSession(Arrays.asList(surfaceTexture), new CameraCaptureSession.StateCallback() {
//                @Override
//                public void onConfigured(@NonNull CameraCaptureSession session) {
//                    captureSession = session;
//                    // 进行拍照
//                    captureStillPicture();
//                }
//
//                @Override
//                public void onConfigureFailed(@NonNull CameraCaptureSession session) {
//                    // 处理配置失败的情况
//                }
//            }, null);
//        } catch (CameraAccessException e) {
//            e.printStackTrace();
//        }
//    }
//
//    private void captureStillPicture() {
//        try {
//            final CaptureRequest.Builder captureBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
//            captureBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
//            //自动曝光
//            captureBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);
//            // 设置拍照的格式和输出路径
//            captureBuilder.set(CaptureRequest.JPEG_QUALITY, (byte) 100);
//            ImageReader imageReader = ImageReader.newInstance(1920, 1080, ImageFormat.JPEG, 2);
//            ImageReader.OnImageAvailableListener readerListener = new ImageReader.OnImageAvailableListener() {
//                @Override
//                public void onImageAvailable(ImageReader reader) {
//                    Image image = null;
//                    try {
//                        image = reader.acquireLatestImage();
//                        ByteBuffer buffer = image.getPlanes()[0].getBuffer();
//                        byte[] bytes = new byte[buffer.capacity()];
//                        buffer.get(bytes);
//                        saveImage(bytes);
//                    } finally {
//                        if (image != null) {
//                            image.close();
//                        }
//                    }
//                }
//
//                private void saveImage(byte[] bytes) {
//                    // 将字节保存为文件
//                }
//            };
//            captureBuilder.addTarget(imageReader.getSurface());
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//}
